using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace WXB.RenderNode
{
	struct sInitCartonData
	{
        public List<Image> images;
        public float speed;
        public string name;
	}
	
	public class CartoonNode : NodeBase
	{
		const String NodeType = "CartoonNode";

		public CartoonNode()
		{
			d_speed = 0.2f;
			d_nCurFrame = 0;
		}

		public override string getType()
		{
			return NodeType;
		}

		public override NodeBase clone()
		{
			CartoonNode node = new CartoonNode();
			node.d_images = d_images;
			node.d_speed = d_speed;
			node.d_nCurFrame = 0;
			node.d_name = d_name;

			return node;
		}

        public override void init(object obj)
		{
			sInitCartonData initObj = (sInitCartonData)(obj);
			d_images = initObj.images;
			d_speed = initObj.speed;
			d_name = initObj.name;
			d_speed = 1 / d_speed;
		}

        public override string getAppend()
        {
            return "";
        }

		public override bool update(float fTime)
		{
			d_elapsed += fTime;
			int nframe = d_nCurFrame;
			while (d_elapsed > d_speed)
			{
				d_nCurFrame++;
				d_elapsed -= d_speed;
				if (d_nCurFrame >= d_images.Count)
					d_nCurFrame = 0;
			}

			if (nframe == d_nCurFrame)
				return false;

            if (UIWidgetList.Count > 0)
            {
                UISprite sprite = UIWidgetList[0] as UISprite;
                sprite.atlas = d_images[d_nCurFrame].atlas;
                sprite.spriteName = d_images[d_nCurFrame].name;
            }

			return true;
		}
		
		public override float getHeight()
		{
            Image pImage = d_images[d_nCurFrame];
            if (pImage != null)
                return pImage.getHeight();
			return 0;
		}

		public override float getWidth()
		{
            Image pImage = d_images[d_nCurFrame];
            if (pImage != null)
                return pImage.getWidth();
			return 0;
		}

		public override string getShowText()
		{
			string showText = ("#");
			showText += d_name;
			return showText; 
		}

        public override void render(RenderCache cache, ref float  x, ref uint yline, float zOffset, List<Vector2> vLineSize, Rect targetRect, Rect cliptRect, HorzFormatting xFormatting, float offsetX, float offsetY, float fAlpha)
        {
			Image pImage = d_images[d_nCurFrame];
			float width  = getWidth();
			float height = getHeight();

            float maxWidth = targetRect.width;

			if (x + width > maxWidth)
			{
				x = 0;
				yline++;
			}

			d_beginLine = yline;
			d_endLine   = yline;

			float alignedX = AlignedFormatting(xFormatting, maxWidth, vLineSize[(int)(yline)].x);

			float y_offset = offsetY;
			for (int i = 0; i < yline; ++i)
				y_offset += vLineSize[i].y;

			y_offset += (vLineSize[(int)(yline)].y - height);
            Rect areaRect = new Rect(x + offsetX + alignedX, y_offset, width, height);
            Color color = Color.white;
            color.a = fAlpha;
            cache.cacheImage(this, pImage, areaRect, color, d_bGlint);

			x += width;

			if (d_bNewLine)
			{
				x = 0;
				yline++;
			}
		}

		private List<Image> d_images;
		private float       d_speed;
		private int         d_nCurFrame;
		private string      d_name;
	};
}
